package com.ouman.luoliluoli;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.ouman.luoliluoli.adapters.SponsorRecyclerViewAdapter;
import com.ouman.luoliluoli.models.SponsorModel;
import com.ouman.luoliluoli.utils.ThemeChanger;

import java.util.ArrayList;
import java.util.List;

public class SponsorActivity extends AppCompatActivity {

    private List<SponsorModel> sponsorArray;
    private RecyclerView sponsorRecyclerView;
    private SponsorRecyclerViewAdapter adapter;
    private FloatingActionButton fab;

    private Button buttonWeChat;
    private Button buttonAlipay;

    private int themeColor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ThemeChanger.applyTheme(this, null);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sponsor);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_settings);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("捐赠我们");

        TypedValue typedValue = new TypedValue();
        SponsorActivity.this.getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
        themeColor = typedValue.data;
        System.out.println(themeColor);
        System.out.println("[IMPORTANT] got activity primary color: " + themeColor);

        getWindow().getDecorView().setBackgroundColor(themeColor);
        getWindow().setNavigationBarColor(themeColor);


        buttonWeChat = (Button) findViewById(R.id.buttonSponsorWechat);
        buttonAlipay = (Button) findViewById(R.id.buttonSponsorAlipay);
        buttonWeChat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipboardManager cbm = (ClipboardManager)SponsorActivity.this.getSystemService(Context.CLIPBOARD_SERVICE);
                final android.content.ClipData clipData = android.content.ClipData
                        .newPlainText("WeChat", "jintianiloveu");
                cbm.setPrimaryClip(clipData);
                Toast.makeText(SponsorActivity.this, "微信号已复制，给我们赞助后，我们会定期把名单加到致谢里面哦", Toast.LENGTH_SHORT).show();
            }
        });

        buttonAlipay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipboardManager cbm = (ClipboardManager)SponsorActivity.this.getSystemService(Context.CLIPBOARD_SERVICE);
                final android.content.ClipData clipData = android.content.ClipData
                        .newPlainText("Alipay", "#吱口令#长按复制此条消息，打开支付宝即可添加我为好友MmnHBv82e1");
                cbm.setPrimaryClip(clipData);
                Toast.makeText(SponsorActivity.this, "支付宝吱口令，打开支付宝添加好友吧，我们会定期把赞助者加入致谢名单哦", Toast.LENGTH_SHORT).show();
            }
        });



        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "LuoLiLuoli保障您的支付安全，请放心捐赠", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        sponsorArray = new ArrayList<>();
        SponsorModel sponsor = new SponsorModel();
        sponsor.setSponsor_name("大熊");
        sponsor.setSponsor_extra_word("LuoliLuoli设计很好，我很支持，希望走下去");
        sponsorArray.add(sponsor);

        SponsorModel sponsor2 = new SponsorModel();
        sponsor2.setSponsor_name("大熊");
        sponsor2.setSponsor_extra_word("LuoliLuoli设计很好，我很支持，希望走下去");
        sponsorArray.add(sponsor2);

        SponsorModel sponsor3 = new SponsorModel();
        sponsor3.setSponsor_name("大熊");
        sponsor3.setSponsor_extra_word("LuoliLuoli设计很好，我很支持，希望走下去");
        sponsorArray.add(sponsor3);

        SponsorModel sponsor4 = new SponsorModel();
        sponsor4.setSponsor_name("大熊");
        sponsor4.setSponsor_extra_word("LuoliLuoli设计很好，我很支持，希望走下去");
        sponsorArray.add(sponsor4);

        sponsorRecyclerView = (RecyclerView) findViewById(R.id.recyclerViewSponsor);

        LinearLayoutManager manager = new LinearLayoutManager(SponsorActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        sponsorRecyclerView.setLayoutManager(manager);
        sponsorRecyclerView.setItemAnimator(new DefaultItemAnimator());

        adapter = new SponsorRecyclerViewAdapter(SponsorActivity.this, sponsorArray);
        sponsorRecyclerView.setAdapter(adapter);
        sponsorRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            int y;
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == recyclerView.SCROLL_STATE_IDLE && y>0){
                    fab.hide();
                }else if(newState == recyclerView.SCROLL_STATE_IDLE && y<0){
                    fab.show();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                y = dy;
                //上滑隐藏fab
                if (dy > 0)
                {
                    fab.hide();
                }else{
                    fab.show();
                }
            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == android.R.id.home){
            SponsorActivity.this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }



}

